/*
 * @Date: 2025-03-10 09:33:55
 * @LastEditTime: 2025-04-02 14:18:04
 * @FilePath: \LeagueBard\frontend\src\views\matchAssistant\utils\matchAssistant.ts
 * @Description: 对局助手数据获取
 */
import { getCurrentMatchApi } from '@/api/client/matchApi'
import { useClientStore } from '@/stores/client'
import { useMessageListStore } from '@/stores/messgelist'

export default class CurrentMatch {
  private cb: (any) => void
  private currentMatch: any

  constructor(cb) {
    this.cb = cb
    this.currentMatch = {}
  }

  // 是否在英雄选择界面
  static inChampSelect() {
    const { phase } = useClientStore().gameflow || {}
    return phase === 'ChampSelect'
  }

  // 是否在对局中
  static inMatch() {
    const { phase } = useClientStore().gameflow || {}
    return ['GameStart', 'InProgress', 'EndOfGame', 'WaitingForStats', 'PreEndOfGame'].includes(phase)
  }

  getCurrentMatch() {
    const { gameData } = useClientStore().gameflow || {}
    if (CurrentMatch.inChampSelect()) {
      // 英雄选择
      getCurrentMatchApi().then((data) => {
        this.formatCurrentMatch(data)
      })
    } else if (CurrentMatch.inMatch()) {
      // 对局开始
      this.updateCurrentMatch(gameData)
    }

    useMessageListStore().addMessage({
      name: 'CurrentMatch',
      trigger: ({ data }) => {
        // 如果是在英雄选择界面，就获取当前对局数据
        CurrentMatch.inChampSelect() && this.formatCurrentMatch(data)
      },
    })
    useMessageListStore().addMessage({
      name: 'Gameflow',
      trigger: ({ data }) => {
        if (CurrentMatch.inMatch()) {
          // 如果是在对局中，就更新当前对局数据
          this.updateCurrentMatch(data.gameData)
        } else if (!CurrentMatch.inChampSelect()) {
          // 如果不在英雄选择界面，就清空当前对局数据
          this.currentMatch = {}
          this.cb(null)
        }
      },
    })
  }

  // 更新对局数据
  updateCurrentMatch(data) {
    // 查找哪队是对方的队伍 teamOne teamTwo
    const index = data.teamOne.findIndex((item) => {
      return item.puuid === useClientStore().userInfo?.puuid
    })
    let myTeam = []
    let otherTeam = []
    if (index === -1) {
      // 说明 teamOne 是对方的队伍
      myTeam = data.teamTwo
      otherTeam = data.teamOne
    } else {
      // 说明 teamTwo 是对方的队伍
      myTeam = data.teamOne
      otherTeam = data.teamTwo
    }

    if (!this.currentMatch?.gameTeam || this.currentMatch.gameTeam.length === 0) {
      this.currentMatch = {
        gameId: data.gameId || 0,
        gameTeam: [{
          teamId: -1,
          members: myTeam || [],
        }],
      }
    }
    const params = {
      teamId: -2,
      members: otherTeam || [],
    }
    if (this.currentMatch.gameTeam.length === 2) {
      this.currentMatch.gameTeam[1] = params
    } else if (this.currentMatch.gameTeam.length === 1) {
      this.currentMatch.gameTeam.push(params)
    }
    this.cb(this.currentMatch)
  }

  // 整理获取的对局数据
  formatCurrentMatch(data) {
    this.currentMatch = {
      gameId: data.gameId || 0,
      gameTeam: [
        {
          teamId: -1,
          members: data.myTeam || [],
        },
      ],
    }
    this.cb(this.currentMatch)
  }
}
